//
//  UploadImageHandle.m
//
//  Created by mac on 16/3/7.
//  Copyright © 2016年 DingGouBai. All rights reserved.
//

#import "UploadImageHandle.h"
#import <HXPhotoPicker.h>
#import <UIKit/UIKit.h>

@interface UploadImageHandle()<HXCustomNavigationControllerDelegate,HXCustomCameraViewControllerDelegate>
@property (nonatomic,strong) SuccessBlock successBlock;
@property (nonatomic,strong) FailureBlock failureBlock;
@property (nonatomic,assign) NSInteger maxUploadNumber;
@property (nonatomic,assign) NSInteger editTpye;
@property (strong, nonatomic) HXPhotoManager *manager;
//@property (strong, nonatomic) HXDatePhotoToolManager *toolManager;
@end

@implementation UploadImageHandle
DEFINE_SINGLETON_FOR_CLASS(UploadImageHandle)
//- (HXDatePhotoToolManager *)toolManager {
//    if (!_toolManager) {
//        _toolManager = [[HXDatePhotoToolManager alloc] init];
//    }
//    return _toolManager;
//}

- (HXPhotoManager *)manager {
    if (!_manager) {
        _manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
        _manager.configuration.openCamera = NO;
        _manager.configuration.saveSystemAblum = NO;
        _manager.configuration.lookGifPhoto = NO;
        _manager.configuration.selectTogether = NO;
        _manager.configuration.singleSelected = YES;
        _manager.configuration.singleJumpEdit = YES;
        _manager.configuration.movableCropBox = YES;
        _manager.configuration.maxNum = 1;
        _manager.configuration.photoMaxNum = 1;
        _manager.configuration.movableCropBoxEditSize = NO;
        _manager.configuration.movableCropBoxCustomRatio = CGPointMake(300, 300);
        _manager.configuration.rowCount = 4;
        _manager.configuration.reverseDate = YES;
        _manager.configuration.showDateSectionHeader = NO;
        _manager.configuration.photoCanEdit = NO;
        _manager.configuration.videoCanEdit = NO;
        _manager.configuration.themeColor = [UIColor blackColor];
//        _manager.configuration.restoreNavigationBar = YES;
    }
    return _manager;
}




- (void)uploadImageWithMaxSelectNumber:(NSInteger)number editTpye:(NSInteger)editTpye VC:(UIViewController *)VC Sucess:(SuccessBlock)sucess Failure:(FailureBlock)failure
{
    if (number) {
        self.maxUploadNumber =number;
    }else{
        self.maxUploadNumber = 1;
    }
    if (editTpye) {
        self.editTpye = editTpye;
    }else{
        self.editTpye = 0;
    }
    self.manager.configuration.singleJumpEdit = self.editTpye;
    if (editTpye==0) {
        [_manager clearSelectedList];
        _manager.configuration.maxNum = 9;
        _manager.configuration.photoMaxNum = 9;
        _manager.configuration.openCamera = YES;
        _manager.configuration.singleSelected = NO;
        
    }else{
        [_manager clearSelectedList];
        _manager.configuration.maxNum = 1;
        _manager.configuration.photoMaxNum = 1;
        _manager.configuration.openCamera = NO;
        _manager.configuration.singleSelected = YES;
        _manager.configuration.singleJumpEdit = YES;
    }
    if (sucess) {
        self.successBlock = sucess;
    }
    
    if (failure) {
        self.failureBlock = failure;
    }
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:NSLocalizedString(@"上传图片", nil) preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:NSLocalizedString(@"取消", nil) style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *paizhao = [UIAlertAction actionWithTitle:NSLocalizedString(@"拍照", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [VC.view hx_showImageHUDText:NSLocalizedString(@"此设备不支持相机!", nil)];
            if (self.failureBlock) {
                self.failureBlock(@"该设备不支持相机!");
            }
            return;
        }
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"无法使用相机", nil) message:NSLocalizedString(@"请在设置-隐私-相机中允许访问相机", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:NSLocalizedString(@"设置", nil), nil];
            [alert show];
            
#pragma clang diagnostic pop
            
            return;
        }
        [VC hx_presentCustomCameraViewControllerWithManager:self.manager delegate:self];
    }];
    UIAlertAction *xiangce = [UIAlertAction actionWithTitle:NSLocalizedString(@"相册", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [VC hx_presentSelectPhotoControllerWithManager:self.manager delegate:self];
#pragma clang diagnostic pop
        
        
    }];
    [alertVc addAction:cancle];
    [alertVc addAction:paizhao];
    [alertVc addAction:xiangce];
    [VC presentViewController:alertVc animated:YES completion:nil];
    
}

#pragma mark - UIImagePickerControllerDelegate

- (void)customCameraViewController:(HXCustomCameraViewController *)viewController didDone:(HXPhotoModel *)model{
    model.selectIndexStr = @"1";
    NSArray *selectedArray = @[model];
    [selectedArray hx_requestImageWithOriginal:YES completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
        if (self.successBlock) {
            self.successBlock(imageArray);
        }
    }];
//    [self.toolManager getSelectedImageList:@[model] success:^(NSArray<UIImage *> *imageList) {
//        if (self.successBlock) {
//            self.successBlock(imageList);
//        }
//    } failed:^{
//
//    }];
}



-(void)photoNavigationViewController:(HXCustomNavigationController *)photoNavigationViewController didDoneAllList:(NSArray<HXPhotoModel *> *)allList photos:(NSArray<HXPhotoModel *> *)photoList videos:(NSArray<HXPhotoModel *> *)videoList original:(BOOL)original{
    
    [photoList hx_requestImageWithOriginal:YES completion:^(NSArray<UIImage *> * _Nullable imageArray, NSArray<HXPhotoModel *> * _Nullable errorArray) {
        if (self.successBlock) {
            self.successBlock(imageArray);
        }
    }];
//    [self.toolManager getSelectedImageList:allList requestType:HXDatePhotoToolManagerRequestTypeOriginal success:^(NSArray<UIImage *> *imageList) {
//        NSSLog(@"%@",imageList);
//        if (self.successBlock) {
//            self.successBlock(imageList);
//        }
//    } failed:^{
//
//    }];
    
    
    
}

@end

